package com.sunshine.user.controller;


import com.sunshine.user.entity.Merchant;
import com.sunshine.user.service.MerchantService;
import com.sunshine.util.TokenUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import sunshine.commcon.util.entity.Result;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/merchant")
public class MerchantController {
    private static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Resource
    private MerchantService merchantService;


    @PostMapping("/createMerchant")
    public Result<Boolean> createMerchant(@RequestBody Merchant merchant) {
        log.info("创建商户");
        return merchantService.createMerchant(merchant);
    }

    @PostMapping("/loginMerchant")
    public Result<Merchant> login(@RequestBody Merchant merchant) {
        log.info("商户登录");
        return merchantService.login(merchant.getMerchantName(), merchant.getMerchantPassword());
    }


    /**
     * 修改商家密码
     */
    @PostMapping("/updatePassword")
    public Result<Boolean> updatePassword(HttpServletRequest request,
                                          @RequestParam String oldPassword,
                                          @RequestParam String newPassword) {
        log.info("商户修改密码");
        // 从token中提取商家ID
        Long merchantId = TokenUtil.getUserIdFromRequest(request);
        if (merchantId == null) {
            return Result.fail("商户未登录或token无效");
        }

        return merchantService.updatePassword(merchantId, oldPassword, newPassword);
    }


}
